package cn.javabb.phonebook.controller;

import cn.javabb.phonebook.constant.SysConstant;
import cn.javabb.phonebook.entity.Admin;
import cn.javabb.phonebook.entity.R;
import cn.javabb.phonebook.service.AdminService;
import cn.javabb.phonebook.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2020/05/02 22:48
 */
@RestController
public class LoginController {
    @Autowired
    private AdminService adminService;

    /**
     * 管理员登陆+签发token
     * @param admin
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestBody Admin admin) {
        Admin u = adminService.login(admin);

        if (u == null) {
            return R.error("用户名或密码错误");
        }else{
            String token = JwtUtils.createJWT(String.valueOf(u.getId()), u.getUsername(), SysConstant.JWT_TTL);
            R r = new R();
            r.put("token",token);
            return r;
        }
    }
}
